package graphics.object.testing;

import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2i;

import graphics.GLObjectManager.RenderPriority;
import graphics.TestingGraphics;
import graphics.object.GLRectangle;
import graphics.object.interfaces.GLKeyboardListener;
import graphics.object.interfaces.GLTickListener;

import java.awt.Color;

public class GLArrowMovementRect extends GLRectangle implements GLKeyboardListener, GLTickListener{

    private static final long serialVersionUID = 1L;

    public Color rectcolor;
    public Color renderColor;

    private boolean isMoving = false;
    private long lastMove = 0;
    private int moveSpeed = 5;
    private int moveAmount = 10;

    private int xMove = 0;
    private int yMove = 0;

    public GLArrowMovementRect(Color rectcolor, int x, int y, int width, int height){
        super(x, y, width, height);
        setRenderPriority(RenderPriority.LOWEST);
        this.rectcolor = rectcolor;
        renderColor = rectcolor;
        lastMove = TestingGraphics.getTime();
    }

    public void onRender(){
        glDisable(GL_BLEND);
        glDisable(GL_TEXTURE_2D);

        glBegin(GL_QUADS);
        glColor3f(renderColor.getRed() / 255f, renderColor.getGreen() / 255f, renderColor.getBlue() / 255f);
        glVertex2i(this.x, this.y);
        glVertex2i(this.x + width, this.y);
        glVertex2i(this.x + width, this.y + this.height);
        glVertex2i(this.x, this.y + this.height);
        glEnd();

        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
    }

    public void onKeyPress(int id) {
        switch(id){
        case 17: // W
            yMove -= 1;
            break;
        case 30: // A
            xMove -= 1;
            break;
        case 31: // S
            yMove += 1;
            break;
        case 32: // D
            xMove += 1;
            break;
        default:
            break;
        }
        isMoving = xMove != 0 || yMove != 0;
    }

    public void onKeyRelease(int id) {
        switch(id){
        case 17: // W
            yMove += 1;
            break;
        case 30: // A
            xMove += 1;
            break;
        case 31: // S
            yMove -= 1;
            break;
        case 32: // D
            xMove -= 1;
            break;
        default:
            break;
        }
        isMoving = xMove != 0 || yMove != 0;
    }

    public void onTick(long currentTime, long deltaTime) {
        if(isMoving){
            long deltaMove = currentTime - lastMove;
            if(deltaMove >= moveSpeed){
                lastMove = currentTime + moveSpeed;
                this.x += xMove * moveAmount;
                this.y += yMove * moveAmount;
            }
        }
    }
}
